﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace P20_6 {
    internal class Program {
        static void Main(string[] args) {

            /**
             * customer：事件拥有者
             * waiter：事件的响应者
             * waiter.Action：事件处理器
             * customer.Order += waiter.Action;：事件订阅
             * customer.Order：事件
             */
            Customer customer = new Customer();
            Waiter waiter = new Waiter();
            //绑定事件
            customer.Order += waiter.Action;
            customer.Action();
            customer.PayTheBill();
            Console.ReadLine();
        }
    }



    /**
     * 事件类必须继承自 EventArgs 父类
     */
    public class OrderEventArags : EventArgs {
        public string DishName { get; set; }

        public string Size { get; set; }

    }

    //声明委托
    public delegate void OrderEventHandler(Customer customer, OrderEventArags e);
    public class Customer {
       
        //声明事件
        public event OrderEventHandler Order;
        public double Bill { get; set; }

        public void PayTheBill() {
            Console.WriteLine("I will pay ${0}", this.Bill);
        }

        public void Walkln() {
            Console.WriteLine("walk into the restaurant.");
        }

        public void SitDown() {
            Console.WriteLine("Sit down");
        }

        public void Think() {

            for (int i = 0; i < 5; i++) {
                Console.WriteLine("Let me think ......");
                Thread.Sleep(1000);
            }

            if (this.Order != null) {
                OrderEventArags e = new OrderEventArags();
                e.DishName = "kongbao chicken";
                e.Size = "large";
                //这里的this指向的是 Customer 这个类，因为事件就是Customer类的 
                this.Order.Invoke(this, e);
            }
        }

        public void Action() {
            Console.ReadLine();
            this.Walkln();
            this.SitDown();
            this.Think();
        }
    }

    //服务生类（订阅者）
    public class Waiter {
        //事件处理器
        public void Action(Customer customer, OrderEventArags e) {
            Console.WriteLine("I will server you the dish- {0}", e.DishName);
            double price = 10;
            switch (e.Size) {
                case "smail":
                    price = price * 0.5;
                    break;
                case "large":
                    price = price * 1.5;
                    break;
                default:
                    break;
            }
            customer.Bill = price;
        }
    }
}
